<script type="text/javascript">
    {literal}
        
    function changeOriginalAmount(tariff, amount) {
        //ramount_original
        if (tariff==6){
            $('#ramount_original_edit').val("0");
        } else {
            $('#ramount_original_edit').val(amount);
        }
    }
        
    function showRecordForm(){
        var display = $(".inlineFormTr").css("display");
        if (display == 'none')
            if ($.browser.msie)
                $(".inlineFormTr").css("display", "block");
            else
                $(".inlineFormTr").css("display", "table-row");
        else
            $(".inlineFormTr").css("display", "none");
    }
        
    function sendAction(action){
        $("#action").val(action);
        $("#editing").submit();    
    }    
        
    function showOther(){
        var display = $("#otherTable").css("display");
        if (display == "none"){
            $("#other").empty();
            $("#other").append("- Task features");
            $("#otherTable").show();
            $("#showOther").val("1");
        } else {
            $("#other").empty();
            $("#other").append("+ Task features");
            $("#otherTable").hide();
            $("#showOther").val("0");
        }
    }
    
    function showTaskTypeTable(id){
        $("#taskType").removeClass("bgSpecial1");
        $(".external").hide();    
        if (id==8 || id==9){
            $("#taskType").addClass("bgSpecial1");
            $("#taskType"+id).show();
        }
    }
    
    function change_responsible(id){
        if (id!=""){
            $("#responsible option[value="+id+"]").attr("selected", "selected");
            $("#worker option[value="+id+"]").attr("selected", "selected");
        } else {
            $("#responsible option[value=-1]").attr("selected", "selected");
            $("#worker option[value=-1]").attr("selected", "selected");
        }
    }
        
    function changeTariffs(value, target){
        if (typeof(target) == 'undefined')
            target = 'rtariff';
        
        if (value!=-1){
            
            var data = {
                action : 'change_tariffs',
                params: {
                    type: value
                }
            }
                
            var result = false;

            ajax(data, function (data) {
                if (data.result==0){
                    $("#"+target).empty();
                    $("#"+target).append('<option value="-1">Please select</option>');
                    result = false;
                } else {
                    $("#"+target).empty();
                    $("#"+target).append('<option value="-1">Please select</option>');
                    $("#"+target).append(data.options)
                    result = true;
                }
            });

            return result;
            
        }
    }
        
    function fillPrice(value, target){
        if (typeof(target) == 'undefined')
            target = 'rprice';
        if (value!=-1){
            
            var data = {
                action : 'fill_price',
                params: {
                    tariff: value
                }
            }

            var result = false;

            ajax(data, function (data) {
                if (data.result==0){
                    $("#"+target).val("0.00");
                    result = false;
                } else {
                    $("#"+target).val(data.price);
                    makeFloat(".double",2);
                    result = true;
                }
            });

            return result;
            
        }
    }
        
    $(document).ready(function() {
        $("input[type='text']").keyup(function(){
            count_tsdr_description_length($(this), '{/literal}{$smarty.const.TSDR_DESCRIPTION_MAX_LENGTH}{literal}');
        });
        
        $("#created").datepicker({ 
            dateFormat: "dd/mm/yy",
            firstDay: 1      
        });
        $("#deadline").datepicker({ 
            dateFormat: "dd/mm/yy",
            firstDay: 1      
        });
        $("#date_of_action").datepicker({ 
            dateFormat: "dd/mm/yy",
            firstDay: 1      
        }); 
        $("#delivery").datepicker({ 
            dateFormat: "dd/mm/yy",
            firstDay: 1      
        }); 
        $(".float").change(function() {
            makeFloat(".float",1);
        });
        $("#rdate").datepicker({ 
            dateFormat: "dd/mm/yy",
            firstDay: 1      
        });
        $("#rdate_edit").datepicker({ 
            dateFormat: "dd/mm/yy",
            firstDay: 1      
        });
        $(".double").change(function() {
            makeFloat(".double",2);
        });
        $("#fulltext").keyup(function(event){
            if(event.keyCode == 13){
                setFulltext($('#fulltext').val(),'timesheets_fulltext')
            }
        });
    {/literal}
    {if isset($smarty.request.add) && $smarty.request.add=='new'}
        {literal}
                showRecordForm();
                unexpended($('#rdate').val(), $('#rclient').val());
        {/literal}
    {/if}
    {if isset($smarty.request.editRecord)}
        {literal}
                unexpended($('#rdate_edit').val(), $('#rclient_edit').val());
        {/literal}
    {/if}
    {literal}   
        showTaskTypeTable({/literal}{$data.task_data.type}{literal});
        $("#rdate").focus();
    });
    {/literal}
</script>
<h2 class="pageTitle">Timesheets</h2>
{include $data.pathToError}
<div class="menu-list">
    Month: 
    <select size="1" onchange="filter('month',this.value,'timesheets_filter')">
        <option value="-1">-</option>
        {foreach from=$data.months item="month" key="i"}
            <option value="{$i}">{$month}</option>
        {/foreach}
    </select>
    Year: 
    <select size="1" onchange="filter('year',this.value,'timesheets_filter')">
        <option value="-1">-</option>
        {foreach from=$data.years item="year" key="i"}
            <option value="{$i}">{$year}</option>
        {/foreach}
    </select>
    Fulltext search: <input id="fulltext" type="text" value="{if $data.currentFulltext != ''}{$data.currentFulltext}{/if}" /><input value="Search" type="button" onclick="setFulltext($('#fulltext').val(),'timesheets_fulltext')" />
    <div style="float: right;">
        <a target="_blank" href="{$data.root}excel.php?table=timesheets">
            <img src="{$data.root}images/excel.png" title="export to excel" alt="" height="20" width="20" />
        </a>
    </div>
</div>
{if $data.currentFilter || $data.currentFulltext!=''}
    <div class="menu-list bgDark">
        <b>Current filter:</b> 
        <ul>
            {foreach $data.currentFilter as $filter => $value}
                <li>{$filter} = <i>{$value}</i> [<a href="javascript: cancelFilter('{$filter}','timesheets_filter')" class="baseLink">Cancel</a>]</li>
            {/foreach}
            {if $data.currentFulltext != ''}
                <li>Fulltext = <i>{$data.currentFulltext}</i> [<a href="javascript: cancelFulltext('timesheets_fulltext')" class="baseLink">Cancel</a>]</li>
            {/if}
        </ul>
    </div>
{/if}

{if isset($smarty.request.add) && $smarty.request.add=='new'}
    <form class="adding" action="{$data.root}?input=timesheets" method="post" onsubmit="return checkAdd_record(
                                                                    $('#rdate').val(), 
                                                                    $('#rclient').val(),
                                                                    $('#rtype').val(),
                                                                    $('#ramount').val(),
                                                                    $('#rprice').val(),
                                                                    $('#rdescription').val(),
                                                                    $('#ramount_original').val(),
                                                                    $('#rtariff').val()
                                                                                   )">
        <table class="inlineForm bgGray">
            <tr>
                <th style="width: 80px" ></th>
                <th style="width: 120px" ></th>
                <th style="width: 150px"></th>
                <th style="width: 80px"></th>
                <th style="width: 120px" ></th>
                <th style="width: 200px" ></th>
                <th style="width: 80px"></th>
                <th style="width: 120px" ></th>
                <th style="width: 20px"></th>
                <th style="width: 50px"></th>
                <th style="width: 50px"></th>
            </tr>
            <tr class="inlineFormTr" style="display: none">
                <td>
                    <input style="width: 80px" name="record[date]" id="rdate" type="text" value="{$data.prefill.date}" onchange="unexpended($('#rdate').val(), $('#rclient').val())" />
                </td>
                <td>
                    <select style="width: 120px" name="record[client]" id="rclient" size="1"  onchange="unexpended($('#rdate').val(),$('#rclient').val())">
                        <option value="-1">Please select</option>
                        {foreach $data.clients as $key => $client}
                            <option {if $smarty.session.user.user_type_id!=$smarty.const.USER_TYPE_CLIENT && $client.bad_payer==1}class="badPayer"{/if} {if $data.prefill.client==$client.klient_id}selected="selected"{/if} value="{$client.klient_id}">{$client.name}</option>
                        {/foreach}
                    </select>
                </td>
                <td>
                    <select style="width: 150px" name="record[type]" id="rtype" size="1" onchange="changeTariffs(this.value)">
                        <option value="-1">Please select</option>
                        {foreach $data.rtypes as $key => $type}
                            <option {if $type.Id==$data.prefill.type}selected="selected"{/if} value="{$type.Id}">{$type.type_of_record}</option>
                        {/foreach}
                    </select>
                </td>
                <td>
                    <input style="width: 80px" name="record[amount]" id="ramount" class="float" type="text" value="{$data.prefill.amount}"  />
                    <input id="ramount_original" type="hidden" value="0"  />
                </td>
                <td>
                    <select style="width: 120px" name="record[tariff]" id="rtariff" size="1" onchange="fillPrice(this.value); fillDescription('#rdescription', this.value)">
                        <option value="-1">Please select</option>
                        {foreach $data.rtariffs as $key => $tariff}
                            <option {if $tariff.Id==$data.prefill.tariff}selected="selected"{/if} value="{$tariff.Id}">{$tariff.title}</option>
                        {/foreach}
                    </select>
                </td>
                <td>
                    <input style="width: 200px" name="record[description]" maxlength="{$smarty.const.TSDR_DESCRIPTION_MAX_LENGTH}" id="rdescription" type="text" value="{if $data.prefill.description}{$data.prefill.description}{/if}"/>
                </td>
                <td>
                    <input style="width: 80px" name="record[price]" id="rprice" class="double" type="text" value="{$data.prefill.unit_price}" />
                </td>
                <td>
                    <select style="width: 120px" name="record[user_id]" id="ruser_id" size="1" {*onchange="showReport(this.value)"*}>
                        {foreach $data.rusers as $key => $user}
                            <option {if $data.prefill.user_id==$user.users_id}selected="selected"{/if} value="{$user.users_id}">{$user.name} {$user.surname}</option>
                        {/foreach}
                    </select>
                </td>
                <td>
                    <input name="record[taskId]" value="{$data.prefill.taskId}" type="hidden" />
                </td>
                <td>
                    <input name="action" value="add_record" type="hidden" />
                    <input type="submit" value="Save" name="add_record" />
                </td>
                <td></td>
            </tr>
            <tr>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
                <th></th>
            </tr>
        </table>
    </form>
{/if}

<form action="{$data.root}?input=timesheets" method="post" onsubmit="return checkAdd_record(
                                                                    $('#rdate_edit').val(), 
                                                                    $('#rclient_edit').val(),
                                                                    $('#rtype_edit').val(),
                                                                    $('#ramount_edit').val(),
                                                                    $('#rprice_edit').val(),
                                                                    $('#rdescription_edit').val(),
                                                                    $('#ramount_original_edit').val(),
                                                                    $('#rtariff_edit').val()
                                                                                   )">
    <table class="list">
        <tr>
            <th style="width: 80px" ><a href="javascript: order('date','timesheets_order')"><label for="rdate">Date*</label> {if $data.currentOrder.0=="date"}<img src="{$data.root}images/{$data.currentOrder.1}.png" height="14" width="14" />{/if}</a></th>
            <th style="width: 120px" ><a href="javascript: order('client','timesheets_order')"><label for="rclient">Client*</label> {if $data.currentOrder.0=="client"}<img src="{$data.root}images/{$data.currentOrder.1}.png" height="14" width="14" />{/if}</a></th>
            <th style="width: 150px" ><a href="javascript: order('type','timesheets_order')"><label for="rtype">Type of record*</label> {if $data.currentOrder.0=="type"}<img src="{$data.root}images/{$data.currentOrder.1}.png" height="14" width="14" />{/if}</a></th>
            <th style="width: 80px" ><a href="javascript: order('amount','timesheets_order')"><label for="ramount">Amount*</label> {if $data.currentOrder.0=="amount"}<img src="{$data.root}images/{$data.currentOrder.1}.png" height="14" width="14" />{/if}</a></th>
            <th style="width: 120px" ><a href="javascript: order('tariff','timesheets_order')"><label for="rtariff">Tariff</label> {if $data.currentOrder.0=="tariff"}<img src="{$data.root}images/{$data.currentOrder.1}.png" height="14" width="14" />{/if}</a></th>
            <th style="width: 200px" ><a href="javascript: order('description','timesheets_order')"><label for="rdescription">Description*</label> {if $data.currentOrder.0=="description"}<img src="{$data.root}images/{$data.currentOrder.1}.png" height="14" width="14" />{/if}</a></th>
            <th style="width: 80px" ><a href="javascript: order('unit_price','timesheets_order')"><label for="rprice">Unit price*</label> {if $data.currentOrder.0=="unit_price"}<img src="{$data.root}images/{$data.currentOrder.1}.png" height="14" width="14" />{/if}</a></th>
            <th style="width: 120px" ><a href="javascript: order('user_id','timesheets_order')"><label for="rclient">Added by*</label> {if $data.currentOrder.0=="user_id"}<img src="{$data.root}images/{$data.currentOrder.1}.png" height="14" width="14" />{/if}</a></th>
            <th style="width: 20px"></th>
            <th style="width: 50px"></th>
            <th style="width: 50px"><a href="javascript: order('attention','timesheets_order')"><label for="rattention">&nbsp;!&nbsp;</label> {if $data.currentOrder.0=="attention"}<img src="{$data.root}images/{$data.currentOrder.1}.png" height="14" width="14" />{/if}</a></th>
        </tr>
        {assign var=counter value=1}
        {foreach $data.records as $key => $record}
            {if $counter==1}
                {assign var=bgClass value='bgDark'}
            {else}
                {assign var=bgClass value='bgLight'}
            {/if}
            <tr class="{$bgClass} row" id="row_{$record.recordId}" onclick="selectRow('{$record.recordId}','{$bgClass}')">
                {if $counter==1}
                    {assign var=counter value=2}
                {else}
                    {assign var=counter value=1}
                {/if}
                {if isset($smarty.request.editRecord) && $smarty.request.editRecord==$record.recordId}
                    <td>
                        <input style="width: 80px" name="record[date]" id="rdate_edit" type="text" value="{$record.date_formated}" onchange="unexpended($('#rdate_edit').val(), $('#rclient_edit').val())" />
                    </td>
                    <td>
                        <select style="width: 150px" name="record[client]" id="rclient_edit" size="1" onchange="unexpended($('#rdate_edit').val(), $('#rclient_edit').val())">
                            <option value="-1">Please select</option>
                            {foreach $data.clients as $key => $client}
                                <option {if $smarty.session.user.user_type_id!=$smarty.const.USER_TYPE_CLIENT && $client.bad_payer==1}class="badPayer"{/if} {if $record.client==$client.klient_id}selected="selected"{/if} value="{$client.klient_id}">{$client.name}</option>
                            {/foreach}
                        </select>
                    </td>
                    <td>
                        <select style="width: 120px" name="record[type]" id="rtype_edit" size="1" onchange="changeTariffs(this.value,'rtariff_edit')">
                            <option value="-1">Please select</option>
                            {foreach $data.rtypes as $key => $type}
                                <option {if $type.Id==$record.type}selected="selected"{/if} value="{$type.Id}">{$type.type_of_record}</option>
                            {/foreach}
                        </select>
                    </td>
                    <td>
                        <input style="width: 80px" name="record[amount]" id="ramount_edit" class="float" type="text" value="{$record.amount}"  />
                        {if $record.tariff == 6}
                            <input id="ramount_original_edit" type="hidden" value="{$record.amount}"  />
                        {else}
                            <input id="ramount_original_edit" type="hidden" value="0"  />
                        {/if}
                    </td>
                    <td>
                        <select style="width: 120px" name="record[tariff]" id="rtariff_edit" size="1" onchange="fillPrice(this.value,'rprice_edit'); fillDescription('#rdescription_edit', this.value);changeOriginalAmount(this.value,'{$record.amount}')">
                            <option value="-1">Please select</option>
                            {foreach $data.rtariffs_edit as $key => $tariff}
                                <option {if $tariff.Id==$record.tariff}selected="selected"{/if} value="{$tariff.Id}">{$tariff.title}</option>
                            {/foreach}
                        </select>
                    </td>
                    <td>
                        <input style="width: 200px" name="record[description]" maxlength="{$smarty.const.TSDR_DESCRIPTION_MAX_LENGTH}" id="rdescription_edit" type="text" value="{$record.description}"/>
                    </td>
                    <td>
                        <input style="width: 80px" name="record[price]" id="rprice_edit" class="double" type="text" value="{$record.unit_price}" />
                    </td>
                    <td>
                        <select style="width: 150px" name="record[user_id]" id="ruser_id_edit" size="1" {*onchange="showReport(this.value)"*}>
                            {foreach $data.rusers as $key => $user}
                                <option {if $record.user_id==$user.users_id}selected="selected"{/if} value="{$user.users_id}">{$user.name} {$user.surname}</option>
                            {/foreach}
                        </select>
                    </td>
                    <td></td>
                    <td>
                        <input name="recordId" value="{$record.recordId}" type="hidden" />
                        <input name="record_taskId" value="{$record.taskId}" type="hidden" />
                        <input name="action" value="edit_record" type="hidden" />
                        <input type="submit" value="Save" name="add_record" />
                    </td>
                    <td></td>
                {else}
                    <td><a class="filterLink" href="javascript: filter('date','{$record.date}','timesheets_filter')">{$record.date_formated}</a></td>
                    <td><a class="filterLink" href="javascript: filter('client','{$record.client}','timesheets_filter')">{$record.client_info.name}</a></td>
                    <td><a class="filterLink" href="javascript: filter('type','{$record.type}','timesheets_filter')">{$record.type_info.type_of_record}</a></td>
                    <td><a class="filterLink" href="javascript: filter('amount','{$record.amount}','timesheets_filter')">{$record.amount}</a></td>
                    <td><a class="filterLink" href="javascript: filter('tariff','{$record.tariff}','timesheets_filter')">{$record.tariff_info.title}</a></td>
                    <td>
                        {if $record.locked==0}
                            <a class="filterLink" href="{$data.root}?input=timesheets&editRecord={$record.recordId}">{$record.description}</a>
                        {else}
                            {$record.description}
                            <img height="16" title="locked" width="16" src="{$smarty.const.PATH_TO_IMAGES}locked.png" />
                        {/if}
                    </td>
                    <td><a class="filterLink" href="javascript: filter('unit_price','{$record.unit_price}','timesheets_filter')">{$record.unit_price}</a></td>
                    <td><a class="filterLink" href="javascript: filter('user_id','{$record.user_id}','timesheets_filter')">{$record.user_info.name} {$record.user_info.surname}</a></td>
                    <td>
                        {if $record.taskId != 0}
                            <a target="_blank" href="{$data.root}?input=edit_task&amp;task={$record.taskId}" title="to task">
                                <img height="10" width="10" src="{$smarty.const.PATH_TO_IMAGES}arrow.png" title="to task" alt="to task" />
                            </a>
                        {/if}
                    </td>
                    <td>
                        <a href="{$data.root}?input=timesheets&amp;add=new&amp;copy_id={$record.recordId}">
                            <img height="16" width="16" src="{$smarty.const.PATH_TO_IMAGES}copy.png" />
                        </a>
                    </td>
                    <td style="">
                        {if $record.attention==1}
                            <a href="{$data.root}?input=tsdr_more&amp;tsdr_id={$record.recordId}" target="_blank">
                                <img height="16" width="16" src="{$smarty.const.PATH_TO_IMAGES}more-red.png" />
                            </a>
                        {else}
                            <a href="{$data.root}?input=tsdr_more&amp;tsdr_id={$record.recordId}" target="_blank">
                                <img height="16" width="16" src="{$smarty.const.PATH_TO_IMAGES}more.png" />
                            </a>
                        {/if}
                    </td>
                {/if}
            </tr>
        {/foreach}
    </table>
</form>
<div class="menu-list">
    <span class="pager">{$data.pager}</span>
    Records per page
    <select class="pager" size="1" onchange="changeRecordsPerPage(this.value, 'timesheets_pager')">
        <option {if $smarty.session.timesheets_pager==20}selected="selected"{/if} value="20">20</option>
        <option {if $smarty.session.timesheets_pager==50}selected="selected"{/if} value="50">50</option>
        <option {if $smarty.session.timesheets_pager==100}selected="selected"{/if} value="100">100</option>
        <option {if $smarty.session.timesheets_pager==200}selected="selected"{/if} value="200">200</option>
    </select>
    Count: {$data.total_count}
    {*<span><b>Current ordering:</b> <i>{$data.currentOrder}</i> [<a class="baseLink" href="javascript: order('CLEAR','timesheets_order')">Clear ordering</a>]</span>*}
</div>